package com.glsc.ngateway.common.base.domain.mysql.gateway.platform;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.glsc.ngateway.common.base.annotation.EncryptField;
import com.glsc.ngateway.common.base.enums.EncryptTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Data
@Table(name = "t_user")
public class User implements Serializable {
    private static final long serialVersionUID = 3L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

   /* @Column(name = "client_id")
    private String clientId;*/

    @Builder.Default
    @Column(name = "enabled")
    private Integer enabled = 1;

    @Builder.Default
    @Column(name = "b_is_del")
    private Integer deleted = 0;

    @ApiModelProperty(value = "【重要】username实是oa账号信息，比如zzp这种")
    @Column(name = "username", unique = true, nullable = false)
    private String username;

    @EncryptField(type = EncryptTypeEnum.AES)
    @Column(name = "password", nullable = false)
    private String password;

    @ApiModelProperty(value = "【重要】name实际是OA用户的中文姓名")
    @Column(name = "name")
    private String name;

    @Column(name = "remark")
    private String remark;

    @Column(name = "user_type")
    private String userType;

    @Column(name = "email")
    private String email;

    @Column(name = "userid")
    private String userid;

    @Column(name = "department")
    private String userDepartment;

    @Transient
    private String departmentId;

    @ManyToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    @JoinTable(name = "t_user_role",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")},
            uniqueConstraints = @UniqueConstraint(columnNames = {"user_id", "role_id"}))
    private Set<Role> roles;


    @Transient
    private Set<Url> urls;

    @Builder.Default
    @Transient
    private Integer disable = 0;

    @Column(name = "create_account", columnDefinition = "varchar(32) comment '创建人'", updatable = false)
    private String createAccount;

    @Column(name = "insert_time", columnDefinition = "datetime comment '创建时间'", updatable = false)
    @org.hibernate.annotations.CreationTimestamp
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    protected LocalDateTime insertTime;

    @Column(name = "update_account", columnDefinition = "varchar(32) comment '修改人'")
    private String updateAccount;

    @Column(name = "update_time", columnDefinition = "datetime comment '更新时间'")
    @org.hibernate.annotations.UpdateTimestamp
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime updateTime;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", enabled=" + enabled +
                ", deleted=" + deleted +
                ", username='" + username + '\'' +
                ", name='" + name + '\'' +
                ", remark='" + remark + '\'' +
                ", userType='" + userType + '\'' +
                ", email='" + email + '\'' +
                ", userid='" + userid + '\'' +
                ", userDepartment='" + userDepartment + '\'' +
                ", departmentId='" + departmentId + '\'' +
                ", roles=" + roles +
                ", urls=" + urls +
                ", disable=" + disable +
                '}';
    }
}
